package com.xzx.shop_front_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzx.shop_front_api.entity.SearchHistory;
import com.xzx.shop_front_api.mapper.SearchHistoryMapper;
import com.xzx.shop_front_api.service.SearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {
    @Autowired
    private SearchHistoryMapper searchHistoryMapper;
    @Override
    public int insertSearchHistory(SearchHistory searchHistory) {
        return searchHistoryMapper.insert(searchHistory);
    }
    //根据member_id查询搜索历史
    @Override
    public List<SearchHistory> getHistoryByMemberId(int memberId) {
        QueryWrapper<SearchHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.orderByDesc("created_time");
        queryWrapper.last("limit 2");
        return searchHistoryMapper.selectList(queryWrapper);
    }

    @Override
    public SearchHistory getHistoryByMemberIdWord(int memberId, String word) {
        QueryWrapper<SearchHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("word", word);
        return searchHistoryMapper.selectOne(queryWrapper);
    }
}
